package org.NooLab.openNLP.lang.en;

import java.util.ArrayList;
import java.util.Vector;

import org.NooLab.nlp.structures.NLPresultSet;
import org.NooLab.openNLP.bst.*;
import org.NooLab.openNLP.components.LanguageProcessorCore;
import org.NooLab.openNLP.lang.TranslationDictSourceFactory;
import org.NooLab.openNLP.lang.TranslationDictSourceIntf;
import org.NooLab.structures.nlp.PoStagged;
 


import opennlp.tools.cmdline.parser.ParserTool;


/**
 * 
 * wordnet online
 * http://wordnetweb.princeton.edu/perl/webwn?s=cultural%20evening
 * 
 * 
 * this class inherits most of the basic methods from  LanguageProcessorCore;
 * here, we just have to provide an implementation of parseText(), which most likely
 * is language dependent.
 * 
 */
public class LanguageProcessorEnglish extends LanguageProcessorCore{

	
	// for simple word by word translations, in order to provide Dornseiffs for other languages than German 
	TranslationDictSourceIntf translator;
	TranslationDictSourceFactory translatorFactory = new TranslationDictSourceFactory();  
	
	
	// for a single pass
	ParseTree pTree = new ParseTree();

	// for multiple parsings
	ParseTrees pTrees;

	
	PoSTags poSTags = new PoSTags();

	BSTparse bstparse;
	BSTtools bsttools;

	
 
	public LanguageProcessorEnglish(Object parentclass, String lang) throws Exception {
		super(parentclass, lang);
	
		bsttools = new BSTtools(lang,null);
		
		// bstparse = new BSTparse( storyline, bsttools, language, out.getPrintlevel() );

		// eg. = "D:/dev/java/data/texter/cfg/PoS/PoS-tags-catalog.txt";
		String filename = basepath + posTagsCatalogFile; 
			   filename = filename.replace("[[lang]]", language);

		poSTags.importPoSTagsCatalog(filename, language);

		translator = (TranslationDictSourceIntf)translatorFactory.getTranslator(language) ;
	}


	
	protected void parseText(String guid, String text) {

		String pstr, textpart;
		ArrayList<String> sentences;

		//
		

		if (posTagger != null) {
			// put words of each sentence into this array
			String[] sent;

			// split into sentences ...
			sentences = detectSentences(text);

			for (int s = 0; s < sentences.size(); s++) {

				textpart = sentences.get(s);

				bstparse.setOriginalText(textpart);
				// surrounds ,.: with blanks
				textpart = _SeparatePunctuation(textpart);

				sent = textpart.split(" ");
				
				// this one creates an array, where the words have been replaced by the PoS tag
				posTags = posTagger.tag(sent);
				posProbs = posTagger.probs();
				topSequences = posTagger.topKSequences(sent);

				//
				establishPoStagSeries(sent, posTags, posProbs);
												 // creating some output ... this is just for debug
												 pstr = _createString(seriesPoStags, " . ", " :: ", true);
												 out.print(3,"PoS :  \n" + pstr);

				// in english we do not need gramm case, since we have a parser available
				// determineCaseforNouns();

				// inserting defaults values to seriesPoStags, where each item refers to 1 word, and
				// the series reflects the word order in the input data
				bsttools.introduceBSTValues(seriesPoStags, poSTags);
  
				bstparse.introduceBindingsComprehensive(seriesPoStags, poSTags);
				
				prepareResults( bstparse.getParseTrees() );

			} // s -> all sentences in imported text

		} // posTagger != null ?
		
		if (parser != null) {
			// additional to the BSTparse, we use the opneNLP parser ...
			
			topParses = ParserTool.parseLine(text, parser, 1);
			topParses[0].show();
			
			// integrate results ... prepareResults( bstparse.getParseTrees() );
		}

		
	}
	


	@Override
	public int getSentencesCount() {
		// TODO Auto-generated method stub
		return 0;
	}



	/** 
	 * put results to nlpResults, which serves as a generally applicable container 
	 * for the results; this will be taken by parent = LingusiticProcessor, and exhibited
	 * by a default API
	 * 
	 */
	protected void prepareResults(ParseTrees pTrees) {
		int p;
		String str;
		NLPresultSet resultSet;
		ParseTree ptree;

		/*
		 * the problem is that each sentence triggers a whole forest (usually
		 * 100 trees) yet, we want to have only 1 tree here...
		 */
		p = 0;
		for (p = 0; p < pTrees.getpTrees().size(); p++) {

			// preparing structure
			resultSet = new NLPresultSet();

			// accessing item
			ptree = pTrees.getpTrees().get(p);

			// transferring results
			str = ptree.getNounPhrase() + " " + ptree.getVerbPhrase();

			resultSet.setMainPhrase(str);

			// storing the set to the imported container "nlpResults"
			nlpResults.getResultSets().add(resultSet);
		}

	}

	
	// ------------------------------------------------------------------------

	private String _createString(ArrayList<PoStagged> sopos, String isep,
			String gsep, boolean usebrackets) {

		String str = "", itstr;
		int n, i;
		PoStagged ptag;

		n = sopos.size();

		for (i = 0; i < sopos.size(); i++) {
			ptag = sopos.get(i);

			itstr = ptag.textual + isep + ptag.syntacticalTag;

			if (usebrackets) {
				itstr = "(" + itstr + ")";
			}
			str = str + itstr;

			if (i < n - 1) {
				str = str + gsep;
			}

		}

		return str;
	}



	@Override
	protected void determineVerbsConjugations( String language,
											   ArrayList<PoStagged> seriespostags) {
		// 
		
	}
}
